package common.kit;

import java.math.BigDecimal;
import java.math.RoundingMode;

import com.jfinal.kit.StrKit;

public class AmountKit {

	public static final BigDecimal FU = new BigDecimal(-1) ;
	public static final BigDecimal ONE_HUNDRED = new BigDecimal(100);
	public static final BigDecimal THOUSAND_ONE = new BigDecimal(0.001);
	public static final BigDecimal HUNDRED_ONE = new BigDecimal(0.01);
	
	public static String format(BigDecimal bigDecimal) {
		if (null == bigDecimal) {
			return null ;
		}
		return bigDecimal.setScale( 2 , RoundingMode.HALF_UP ).toPlainString() ;
	}
	
	/**
	 * 取绝对值
	 * @param amount
	 * @return
	 */
	public static BigDecimal abs(BigDecimal amount) {
		return amount.compareTo(BigDecimal.ZERO) == 1 ? amount : amount.multiply(FU) ;
	}
	
	public static BigDecimal transByIsIncome(BigDecimal amount , boolean isIncome) {
		BigDecimal abs = AmountKit.abs(amount) ;
		return isIncome ? abs : abs.multiply(FU) ;
	}
	
	public static BigDecimal parse(String amount) {
		if (StrKit.isBlank(amount)) {
			return null ;
		}
		try {
			return new BigDecimal(amount) ;
		} catch (Exception e) {
			return null ;
		}
	}
	
	
}
